package com.hele.sts.action.admin;


import java.util.List;
import java.util.Map;

import com.hele.sts.dao.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hele.sts.pub.StringUtil;
import com.hele.sts.webwork.BaseAction;
import com.opensymphony.xwork2.ActionContext;
import org.springframework.beans.factory.annotation.Autowired;

public class QEkUserManagerAction
  extends BaseAction
{
  private static final Logger logger = LoggerFactory.getLogger(QEkUserManagerAction.class);
  String userId;
  String userName;
  String unit;
  String department;
  String certificate;
  String telephone;
  String physicalAddress;
  String ipAddress;
  double discount;
  double blance;
  String errorInfo;
  List ekUserList;
  QEkUser qEkUser;
  String yhlx;
  String UserNameek;
  String fullname;

  @Autowired
  private XtLogDao xtLogDao;
  
  public String getFullname()
  {
    return this.fullname;
  }
  
  public void setFullname(String fullname)
  {
    this.fullname = fullname;
  }
  
  @Override
  public String execute()
    throws Exception
  {
    ActionContext ctx = ActionContext.getContext();
    Map session = ctx.getSession();
    String loginName = (String)session.get("AdminUser.loginName");
    if ((loginName == null) || (loginName.length() < 1))
    {
      setErrorInfo("没有登录");
      return "error";
    }
    return super.execute();
  }
  
  @Override
  public String update()
  {
    ActionContext ctx = ActionContext.getContext();
    Map session = ctx.getSession();
    User adminUser = (User)session.get(("AdminUser"));

    this.qEkUser = QEkUserDao.load(this.userId);
    if ((this.userName != null) && (this.userName.length() > 0)) {
      this.qEkUser.setUserName(this.userName);
    }
    if ((this.unit != null) && (this.unit.length() > 0)) {
      this.qEkUser.setUnit(this.unit);
    }
    if ((this.department != null) && (this.department.length() > 0)) {
      this.qEkUser.setDepartment(this.department);
    }
    if ((this.certificate != null) && (this.certificate.length() > 0)) {
      this.qEkUser.setCertificate(this.certificate);
    }
    if ((this.telephone != null) && (this.telephone.length() > 0)) {
      this.qEkUser.setTelephone(this.telephone);
    }
    if ((this.physicalAddress != null) && (this.physicalAddress.length() > 0)) {
      this.qEkUser.setPhysicalAddress(this.physicalAddress);
    }
    if ((this.ipAddress != null) && (this.ipAddress.length() > 0)) {
      this.qEkUser.setIpAddress(this.ipAddress);
    }
    if (this.discount != 0.0D) {
      this.qEkUser.setDiscount(this.discount);
    }
    if (this.blance != 0.0D) {
      this.qEkUser.setBlance(this.blance);
    }
    boolean b = QEkUserDao.update(this.qEkUser);
    if (!b)
    {
      setErrorInfo("更新就用户操作失败");
      xtLogDao.insertLog("业务日志","后管用户登记失败",adminUser.getUserId(),adminUser.getUserName(),"成功","用户："+adminUser.getUserId()+" 用户登记："+userId+" ",this.getClass().getName());

      return "error";
    }
    xtLogDao.insertLog("业务日志","后管用户登记成功",adminUser.getUserId(),adminUser.getUserName(),"成功","用户："+adminUser.getUserId()+" 用户登记："+userId+" ",this.getClass().getName());

    return "update";
  }
  
  @Override
  public String view()
  {
    logger.debug(this.userId);
    this.qEkUser = QEkUserDao.load(this.userId);
    if (this.qEkUser == null)
    {
      setErrorInfo("该用户ID不存在");
      return "error";
    }
    return "view";
  }
  
  @Override
  public String edit() {
    ActionContext ctx = ActionContext.getContext();
    Map session = ctx.getSession();
    User adminUser = (User) session.get(("AdminUser"));
    //20180926 check the name is unique
    String searchUserId = QEkUserDao.getUseridByUsername(userName);
    if (searchUserId == null) {
      xtLogDao.insertLog("业务日志", "后管用户开户失败", adminUser.getUserId(), adminUser.getUserName(), "成功", "用户：" + adminUser.getUserId() + " 内部用户开户：" + userId + " ", this.getClass().getName());

      return "error";
    }
    if (!"".equals(searchUserId)) {
      setErrorInfo("该用户已存在，请重新开户");
      //return "error";
      return "addEkUser";
    }
    this.qEkUser = createQEkUser();
    boolean b = false;
    QEkUser qEkUser0 = QEkUserDao.load(this.userId);
    if (qEkUser0 == null) {
      b = QEkUserDao.insert(this.qEkUser);
      if (!b) {
        xtLogDao.insertLog("业务日志", "后管用户开户失败", adminUser.getUserId(), adminUser.getUserName(), "成功", "用户：" + adminUser.getUserId() + " 内部用户开户：" + userId + " ", this.getClass().getName());

        setErrorInfo("添加新用户操作失败");
        return "error";
      }
      QUser user = QUserDao.load(this.userId);
      if (user == null) {
        boolean bo = QUserDao.insert(createQUser());
        if (!bo) {
          xtLogDao.insertLog("业务日志", "后管用户开户失败", adminUser.getUserId(), adminUser.getUserName(), "成功", "用户：" + adminUser.getUserId() + " 内部用户开户：" + userId + " ", this.getClass().getName());

          setErrorInfo("添加新用户操作失败");
          return "error";
        }
      }
    } else {
      setErrorInfo("该用户已经存在!" + this.userId + "-" + this.userName + "'已存在！");
      return "error";
    }

    xtLogDao.insertLog("业务日志", "后管用户开户成功", adminUser.getUserId(), adminUser.getUserName(), "成功", "用户：" + adminUser.getUserId() + " 内部用户开户：" + userId + " ", this.getClass().getName());

    return "success";
  }
  
  @Override
  public String delete()
  {
    return null;
  }
  
  @Override
  public String getHref()
  {
    String href = "";
    if ((this.userName != null) && (this.userName.length() > 0)) {
      href = href + "&userName=" + this.userName;
    }
    if ((this.unit != null) && (this.unit.length() > 0)) {
      href = href + "&unit=" + this.unit;
    }
    if ((this.certificate != null) && (this.certificate.length() > 0)) {
      href = href + "&certificate=" + this.certificate;
    }
    return href;
  }
  
  @Override
  public int count()
  {
    return QEkUserDao.count(this.userName, this.unit, this.certificate);
  }
  
  @Override
  public String list()
  {
    this.ekUserList = QEkUserDao.load(this.userName, this.unit, this.certificate, getPageNo(), getPageSize());
    logger.debug(this.ekUserList.size() + "---");
    return "list";
  }
  
  QEkUser createQEkUser()
  {
    QEkUser bean = new QEkUser();
    bean.setBlance(this.blance);
    bean.setCertificate(this.certificate);
    bean.setDepartment(this.department);
    bean.setDiscount(this.discount);
    bean.setIpAddress(this.ipAddress);
    bean.setPhysicalAddress(this.physicalAddress);
    bean.setTelephone(this.telephone);
    bean.setUnit(this.unit);
    bean.setFullname(this.fullname);
    if ((this.userId == null) || (this.userId.length() < 1)) {
      this.userId = StringUtil.createUserId();
    }
    bean.setUserId(this.userId);
    bean.setUserName(this.userName);
    //xuning add 20150113
    
    //bean.setDjId(StringUtil.randomStrWithDatetime());
    return bean;
  }
  
  QUser createQUser()
  {
    QUser bean = new QUser();
    bean.setStatus("1");
    bean.setUserId(this.userId);
    bean.setYhlx(this.yhlx);
    bean.setUserName(this.userName);
    return bean;
  }
  
//  public static Logger getLogger()
//  {
//    return logger;
//  }
  
//  public static void setLogger(Logger logger)
//  {
//    logger = logger;
//  }
  
  public double getBlance()
  {
    return this.blance;
  }
  
  public void setBlance(double blance)
  {
    this.blance = blance;
  }
  
  public String getCertificate()
  {
    return this.certificate;
  }
  
  public void setCertificate(String certificate)
  {
    this.certificate = certificate;
  }
  
  public String getDepartment()
  {
    return this.department;
  }
  
  public void setDepartment(String department)
  {
    this.department = department;
  }
  
  public double getDiscount()
  {
    return this.discount;
  }
  
  public void setDiscount(double discount)
  {
    this.discount = discount;
  }
  
  public List getEkUserList()
  {
    return this.ekUserList;
  }
  
  public void setEkUserList(List ekUserList)
  {
    this.ekUserList = ekUserList;
  }
  
  public String getErrorInfo()
  {
    return this.errorInfo;
  }
  
  public void setErrorInfo(String errorInfo)
  {
    this.errorInfo = errorInfo;
  }
  
  public String getIpAddress()
  {
    return this.ipAddress;
  }
  
  public void setIpAddress(String ipAddress)
  {
    this.ipAddress = ipAddress;
  }
  
  public String getPhysicalAddress()
  {
    return this.physicalAddress;
  }
  
  public void setPhysicalAddress(String physicalAddress)
  {
    this.physicalAddress = physicalAddress;
  }
  
  public QEkUser getQEkUser()
  {
    return this.qEkUser;
  }
  
  public void setQEkUser(QEkUser ekUser)
  {
    this.qEkUser = ekUser;
  }
  
  public String getTelephone()
  {
    return this.telephone;
  }
  
  public void setTelephone(String telephone)
  {
    this.telephone = telephone;
  }
  
  public String getUnit()
  {
    return this.unit;
  }
  
  public void setUnit(String unit)
  {
    this.unit = unit;
  }
  
  public String getUserId()
  {
    return this.userId;
  }
  
  public void setUserId(String userId)
  {
    this.userId = userId;
  }
  
  public String getUserName()
  {
    return this.userName;
  }
  
  public void setUserName(String userName)
  {
    this.userName = userName;
  }
  
  public String getUserNameek()
  {
    return this.UserNameek;
  }
  
  public void setUserNameek(String userNameek)
  {
    this.UserNameek = userNameek;
  }
  
  public String getYhlx()
  {
    return this.yhlx;
  }
  
  public void setYhlx(String yhlx)
  {
    this.yhlx = yhlx;
  }
}
